# 查找 Qt Test 模块
find_package(Qt6 COMPONENTS Test QUIET)
if(NOT Qt6Test_FOUND)
    find_package(Qt5 COMPONENTS Test REQUIRED)
endif()

# 测试辅助函数
function(add_powercloud_test TEST_NAME TEST_SOURCE)
    add_executable(${TEST_NAME} ${TEST_SOURCE})
    target_link_libraries(${TEST_NAME}
        PRIVATE
            PowerCloudCore
            Qt::Core
            Qt::Test
    )
    target_include_directories(${TEST_NAME}
        PRIVATE
            ${CMAKE_SOURCE_DIR}/src
    )
    add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
endfunction()

# 添加Modbus通信测试
add_powercloud_test(test_modbus test_modbus.cpp)

# 数据库测试
add_executable(test_database test_database.cpp)
target_link_libraries(test_database
    PRIVATE
        PowerCloudCore
        Qt::Core
        Qt::Sql
)
target_include_directories(test_database
    PRIVATE
        ${CMAKE_SOURCE_DIR}/src
)
add_test(NAME test_database COMMAND test_database)

# 占位测试文件（保留用于快速验证构建系统）
add_executable(test_placeholder test_placeholder.cpp)
target_link_libraries(test_placeholder
    PRIVATE
        PowerCloudCore
        Qt::Core
        Qt::Test
)
add_test(NAME test_placeholder COMMAND test_placeholder)

# ========== 使用示例（Demo）==========
# Modbus 通信使用示例
add_executable(demo_modbus demo_modbus_usage.cpp)
target_link_libraries(demo_modbus
    PRIVATE
        PowerCloudCore
        Qt::Core
)
target_include_directories(demo_modbus
    PRIVATE
        ${CMAKE_SOURCE_DIR}/src
)

# DeviceManager 和 MeterModel 使用示例
add_executable(demo_devicemanager demo_devicemanager_usage.cpp)
target_link_libraries(demo_devicemanager
    PRIVATE
        PowerCloudCore
        Qt::Core
)
target_include_directories(demo_devicemanager
    PRIVATE
        ${CMAKE_SOURCE_DIR}/src
)

# AlarmEngine 使用示例
add_executable(demo_alarmengine demo_alarmengine_usage.cpp)
target_link_libraries(demo_alarmengine
    PRIVATE
        PowerCloudCore
        Qt::Core
)
target_include_directories(demo_alarmengine
    PRIVATE
        ${CMAKE_SOURCE_DIR}/src
)
set_target_properties(demo_alarmengine PROPERTIES AUTOMOC ON)

# BemfaClient 使用示例
add_executable(demo_bemfaclient demo_bemfaclient_usage.cpp)
target_link_libraries(demo_bemfaclient
    PRIVATE
        PowerCloudCore
        Qt::Core
        Qt::Network
)
target_include_directories(demo_bemfaclient
    PRIVATE
        ${CMAKE_SOURCE_DIR}/src
)
set_target_properties(demo_bemfaclient PROPERTIES AUTOMOC ON)

# BemfaClient 协议完整性测试
add_executable(test_bemfaclient_protocol test_bemfaclient_protocol.cpp)
target_link_libraries(test_bemfaclient_protocol
    PRIVATE
        PowerCloudCore
        Qt::Core
        Qt::Network
)
target_include_directories(test_bemfaclient_protocol
    PRIVATE
        ${CMAKE_SOURCE_DIR}/src
)
set_target_properties(test_bemfaclient_protocol PROPERTIES AUTOMOC ON)

# Modbus 从站模拟器（用于虚拟串口测试）
add_executable(modbus_slave_simulator modbus_slave_simulator.cpp)
target_link_libraries(modbus_slave_simulator
    PRIVATE
        Qt::Core
        Qt::SerialPort
)
set_target_properties(modbus_slave_simulator PROPERTIES AUTOMOC ON)

